package com.pay.plat.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pay.entity.SysMenus;
import com.pay.entity.SysRole;
import com.pay.entity.SysUser;
import com.pay.plat.annotation.AuthResource;
import com.pay.plat.mapping.MappingUtils;
import com.pay.plat.service.ManagerService;
import com.pay.service.ISysRoleService;
import com.pay.service.ISysUserService;
import com.pay.utils.ResponseData;

@Controller
public class ManagerController {

	@Autowired
	private ManagerService managerService ;
	
	@Autowired
	private ISysRoleService sysRoleService ;
	@Autowired
	private ISysUserService sysUserService ;
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showUserManager)
	public ModelAndView showUserManager(SysUser sysUser,Integer loginId) {
		List<SysUser> users = managerService.getUserList(sysUser) ;
		ModelAndView m = new ModelAndView(MappingUtils.showUserManager);
		m.addObject("loginId", loginId) ;
		m.addObject("users", users) ;
		m.addObject("sysUser", sysUser) ;
		return m ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showUserAdd)
	public ModelAndView showUserAdd(Integer loginId) { 
		List<SysRole> roles = sysRoleService.list(null);  //所有的角色
		ModelAndView m = new ModelAndView(MappingUtils.showUserAdd);
		m.addObject("roles", roles) ;
		m.addObject("loginId", loginId) ;
		return m ;
	}
	
	@AuthResource(url=MappingUtils.showUserAdd)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doUserAdd,method= {RequestMethod.POST})
	public ResponseData doUserAdd(SysUser sysUser) {
		return managerService.doUserAdd(sysUser) ;
	}
	
	
	@ResponseBody
	@AuthResource
	@RequestMapping(value=MappingUtils.doUserDelete,method= {RequestMethod.POST})
	public ResponseData doUserDelete(String ids,Integer loginId) {
		return managerService.doUserDelete(ids,loginId) ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showUserEdit)
	public ModelAndView showUserEdit(Integer id,Integer loginId) {
		List<SysRole> roles = sysRoleService.list(null);  //所有的角色 
		SysUser user = sysUserService.getById(id);
		ModelAndView m = new ModelAndView(MappingUtils.showUserEdit);
		m.addObject("user", user) ;
		m.addObject("roles", roles) ;
		m.addObject("loginId", loginId) ;
		return m ;
	}
	
	@AuthResource(url=MappingUtils.showUserEdit)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doUserEdit,method= {RequestMethod.POST})
	public ResponseData doUserEdit(SysUser sysUser) {
		return managerService.doUserEdit(sysUser) ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showRoleManager)
	public ModelAndView showRoleManager(Integer loginId) {
		List<SysRole> roles = managerService.getRoleList() ;
		ModelAndView m = new ModelAndView(MappingUtils.showRoleManager);
		m.addObject("loginId", loginId) ;
		m.addObject("roles",roles) ;
		return m ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showRoleAdd)
	public ModelAndView showRoleAdd(Integer loginId) {
		List<SysMenus> menusList = managerService.getResource() ;
		ModelAndView m = new ModelAndView(MappingUtils.showRoleAdd);
		m.addObject("menusList", menusList) ;
		m.addObject("loginId", loginId) ;
		return m ;
	}
	
	@AuthResource(url=MappingUtils.showRoleAdd)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doRoleAdd)
	public ResponseData doRoleAdd(SysRole sysRole,String menuids) {
		return managerService.doRoleAdd(sysRole,menuids) ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showRoleEdit)
	public ModelAndView showRoleEdit(Integer loginId,Integer id) {
		List<SysMenus> menusList = managerService.getResourceByRoleid(id) ;
		SysRole role = managerService.getRoleInfo(id) ;
		ModelAndView m = new ModelAndView(MappingUtils.showRoleEdit);
		m.addObject("menusList", menusList) ;
		m.addObject("loginId", loginId) ;
		m.addObject("role", role) ;
		return m ; 
	}
	
	@AuthResource(url=MappingUtils.showRoleEdit)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doRoleEdit,method= {RequestMethod.POST})
	public ResponseData doRoleEdit(SysRole sysRole,String menuids) {
		return managerService.doRoleEdit(sysRole,menuids) ;
	}
	
	@AuthResource
	@ResponseBody
	@RequestMapping(value=MappingUtils.doRoleDelete)
	public ResponseData doRoleDelete(String ids) {
		return managerService.doRoleDelete(ids) ;
	}
	
	@AuthResource
	@RequestMapping(value=MappingUtils.showEditPass)
	public ModelAndView showEditPass(Integer loginId) {
		ModelAndView m = new ModelAndView(MappingUtils.showEditPass);
		m.addObject("loginId", loginId) ;
		return m ;
	}
	
	@AuthResource(url=MappingUtils.showEditPass)
	@ResponseBody
	@RequestMapping(value=MappingUtils.doEditPass)
	public ResponseData doEditPass(Integer loginId,String origin,String newPass) {
		return managerService.doEditPass(loginId,origin,newPass) ;
		
	}
}
